{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMXbk2W/HLK8zJ1gdc65qw+",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/DanielWarfield1/MLWritingAndResearch/blob/main/GroundX_Court_case.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# GroundX-Court Case\n",
        "a tutorial of creating a chat bot for reading legal transcript"
      ],
      "metadata": {
        "id": "naIdHIIIg6US"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Installing Dependencies"
      ],
      "metadata": {
        "id": "1TkMexokg55r"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "zetSxEAugvfI",
        "outputId": "4fb54f39-c4dd-4295-9ae2-40cfe53580a0"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: groundx-python-sdk in /usr/local/lib/python3.10/dist-packages (1.3.2)\n",
            "Requirement already satisfied: aiohttp<4.0.0,>=3.8.4 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (3.8.6)\n",
            "Requirement already satisfied: certifi>=2023.7.22 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2023.7.22)\n",
            "Requirement already satisfied: frozendict<3.0.0,>=2.3.4 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2.3.8)\n",
            "Requirement already satisfied: python-dateutil<3.0.0,>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (2.8.2)\n",
            "Requirement already satisfied: typing_extensions<5.0.0,>=4.3.0 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (4.5.0)\n",
            "Requirement already satisfied: urllib3<2.0.0,>=1.26.7 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (1.26.18)\n",
            "Requirement already satisfied: validators<0.21.0,>=0.20.0 in /usr/local/lib/python3.10/dist-packages (from groundx-python-sdk) (0.20.0)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (23.1.0)\n",
            "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (3.3.1)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (6.0.4)\n",
            "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (4.0.3)\n",
            "Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.9.2)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.4.0)\n",
            "Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (1.3.1)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil<3.0.0,>=2.8.2->groundx-python-sdk) (1.16.0)\n",
            "Requirement already satisfied: decorator>=3.4.0 in /usr/local/lib/python3.10/dist-packages (from validators<0.21.0,>=0.20.0->groundx-python-sdk) (4.4.2)\n",
            "Requirement already satisfied: idna>=2.0 in /usr/local/lib/python3.10/dist-packages (from yarl<2.0,>=1.0->aiohttp<4.0.0,>=3.8.4->groundx-python-sdk) (3.4)\n"
          ]
        }
      ],
      "source": [
        "!pip install groundx-python-sdk"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install openai"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9EiKXzXlhNZg",
        "outputId": "ea234bb5-d56d-4c49-f3ce-e574cf295939"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (1.1.1)\n",
            "Requirement already satisfied: anyio<4,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)\n",
            "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)\n",
            "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from openai) (0.25.1)\n",
            "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from openai) (1.10.13)\n",
            "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.1)\n",
            "Requirement already satisfied: typing-extensions<5,>=4.5 in /usr/local/lib/python3.10/dist-packages (from openai) (4.5.0)\n",
            "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
            "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.5.0->openai) (1.3.0)\n",
            "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<4,>=3.5.0->openai) (1.1.3)\n",
            "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
            "Requirement already satisfied: httpcore in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (1.0.1)\n",
            "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore->httpx<1,>=0.23.0->openai) (0.14.0)\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Setting Credentials"
      ],
      "metadata": {
        "id": "5G7ZyLY1hVi_"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#copying from google drive to local\n",
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "\n",
        "!cp '/content/drive/My Drive/ColabDatasets/Supreme Court Dobbs Transcript of Arguments.pdf' 'Dobbs.pdf'"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "TcUq0acCh8nc",
        "outputId": "0d944e59-d287-4112-81dd-c7f28a306121"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "\"\"\"Defining GroundX Credentials\n",
        "\"\"\"\n",
        "\n",
        "from groundx import Groundx, ApiException\n",
        "\n",
        "with open (\"/content/drive/My Drive/Colab Notebooks/Credentials/groundXAPIKey.txt\", \"r\") as myfile:\n",
        "    groundx = Groundx(\n",
        "        api_key=myfile.read(),\n",
        "    )\n",
        "print('API Key Loaded!')"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WxHeaZOlhRcb",
        "outputId": "173ae1d6-6f38-49c1-ebc7-3486492ddb44"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "API Key Loaded!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "What to put in the actual article:\n",
        "\n",
        "```\n",
        "from groundx import Groundx\n",
        "\n",
        "groundx = Groundx(\n",
        "    api_key=GROUNDX_API_KEY,\n",
        ")\n",
        "\n",
        "```"
      ],
      "metadata": {
        "id": "PTHUGnfVh10z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "with open (\"/content/drive/My Drive/Colab Notebooks/Credentials/OpenAI-danielDemoKey.txt\", \"r\") as myfile:\n",
        "    OPENAI_API_TOKEN = myfile.read()\n",
        "    os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_TOKEN\n",
        "print('API Key Loaded!')\n",
        "\n",
        "import openai\n",
        "openai.api_key = OPENAI_API_TOKEN"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NI8Zs9lRiKRH",
        "outputId": "a56ed111-5179-4129-d1d1-8edd3d4a6aaf"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "API Key Loaded!\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "What to put in the article:\n",
        "```\n",
        "import openai\n",
        "\n",
        "openai.api_key = OPENAI_API_KEY\n",
        "```"
      ],
      "metadata": {
        "id": "CTSnxDCoii8y"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Cleaning up bucket\n",
        "deleting stuff"
      ],
      "metadata": {
        "id": "fLYNWKfLk_eb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "response = groundx.documents.list()\n",
        "print(response.body)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "D8W3jGm6k_RV",
        "outputId": "7d53b90c-0b0f-4675-f3c9-3559dcfc2464"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'document': {'nextToken': 'MjY5ODQx'}, 'documents': [{'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9', 'fileName': 'Dobbs ', 'fileSize': '17.6 MB', 'fileType': 'pdf', 'bucketId': 6522, 'processId': '58b3273b-3d47-4e3f-b985-060fc30f34a2', 'sourceUrl': 'https://upload.eyelevel.ai/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs+', 'status': 'complete'}]}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# groundx.documents.delete('c931570d-3940-4a33-9e91-f1e7df558892')"
      ],
      "metadata": {
        "id": "4xtMbQTdlDTs"
      },
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Getting Existing Bucket\n",
        "should have one automatically created (the ability to declare a new bucket programatically would be pretty cool)"
      ],
      "metadata": {
        "id": "Yeo8D9qWkT1g"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "#listing currently available buckets\n",
        "bucket_response = groundx.buckets.list()\n",
        "print(bucket_response.body[\"buckets\"])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Y2XvYEtakwdE",
        "outputId": "513ba4b5-9a58-4bbc-8c5c-b0d810e4033d"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "[{'created': '2023-10-19T15:41:48Z', 'bucketId': 6522, 'name': 'default bucket', 'updated': '2023-10-26T14:42:23Z'}]\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Uploading a document\n",
        "https://www.supremecourt.gov/oral_arguments/argument_transcripts/2021/19-1392_bq7d.pdf"
      ],
      "metadata": {
        "id": "-CRnAZPol6F8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "\"\"\"Adding local .pdf file to bucket\n",
        "\"\"\"\n",
        "\n",
        "#adding a transcript of the Dobbs supreme court case to the default bucket.\n",
        "ingest = groundx.documents.upload_local(body=[{\n",
        "    \"blob\": open(\"Dobbs.pdf\", \"rb\"),\n",
        "    \"metadata\": {\n",
        "        \"bucketId\": 6522,\n",
        "        \"fileName\": \"Dobbs \",\n",
        "        \"fileType\": \"pdf\"}\n",
        "    }]\n",
        ")\n",
        "\n",
        "#getting the process id\n",
        "processId = ingest.body['ingest']['processId']\n",
        "print('document upload process Id:')\n",
        "print(processId)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "z_6Ky8lUk8k7",
        "outputId": "5985e336-51a0-4b73-99cf-bed804e04a05"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "document upload process Id:\n",
            "58b3273b-3d47-4e3f-b985-060fc30f34a2\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#still processing document\n",
        "print(groundx.documents.get_processing_status_by_id(process_id=processId).body['ingest']['progress'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "djUk0ulPs9Ak",
        "outputId": "81a15c87-2716-44c3-eb52-0bb4c3393351"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'processing': {'documents': [{'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9', 'fileName': 'Dobbs ', 'fileSize': '556 KB', 'fileType': 'pdf', 'bucketId': 6522, 'processId': '58b3273b-3d47-4e3f-b985-060fc30f34a2', 'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ', 'status': 'processing'}], 'total': 1}}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "#done processing document\n",
        "print(groundx.documents.get_processing_status_by_id(process_id=processId).body['ingest']['progress'])"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "SGLZ2CB5tAGT",
        "outputId": "3d6c269b-4c71-4f7d-e1b4-46e91f439247"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'complete': {'documents': [{'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9', 'fileName': 'Dobbs ', 'fileSize': '17.6 MB', 'fileType': 'pdf', 'bucketId': 6522, 'processId': '58b3273b-3d47-4e3f-b985-060fc30f34a2', 'sourceUrl': 'https://upload.eyelevel.ai/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs+', 'status': 'complete'}], 'total': 1}}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Retreival"
      ],
      "metadata": {
        "id": "q-807q26u4xm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "content_response = groundx.search.content(id=6522, search={\"query\": \"What arguments were posed in banning abortions?\"})\n",
        "content_response.body['search']['results']"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "KTm7VkfctF1t",
        "outputId": "4afefc35-6a0e-4adc-cd05-e2ccfbae53bc"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "[{'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 145.8995,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"that the law applies when an unborn child is undeniably human , when risks to women surge , and when the common abortion procedure is brutal .\\nThe lower courts held that because the law\\nprohibits abortions before viability , it is unconstitutional no matter what .\\nRoe and Casey's core holding ,\\naccording to those courts , is that the people can protect an unborn girl's life when she just barely can survive outside the womb but not any earlier when she needs a little more help . That is the world under Roe and Casey .\\nThat is not the world the Constitution\\npromises . The Constitution places its trust in the people . On hard issue after hard issue , the people make this country work . Abortion is a\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 68.9535,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"overrule the stare decisis section of Casey , you say . And that that's that's what I think\\nis being brought up , and maybe I haven't made it\\nclearer ,\\nbut I've tried to .\\nMS . RIKELMAN : Yes , Your Honor . I\\nthink the point that the Court was making was that the fact that some states may continue to enact laws in the teeth of the Court's precedent has never been enough of a reason to overrule .\\nAnd that's true for a number of decisions that\\nthe Court has issued . The fact that some people continue to disagree with them is not a basis to discard that precedent .\\nCHIEF JUSTICE ROBERTS : Justice\\nThomas , anything further ?\\nJUSTICE THOMAS : Back to my original\\nquestion .\\nIf I were\\nI know your interest\\nhere is in abortion , I\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 65.128845,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"changed , that's not a point in Roe and Casey's favor . They have no basis in the Constitution . They they adopt a right that purposefully leads to the termination of now millions of\\nhuman lives . The\\nif nothing had changed ,\\nthey'd be just as bad as they were 30 years ago , 50 years ago . And now we just have decades of damage , and we have a situation where nearly 30 years after Casey , the Court unfortunately divides over what Casey , the lead case on on\\nin the abortion area , even means .\\nThe lower courts are left not knowing , as I think and I think kind of a\\nwhat to do\\nfundamental problem here is , I think , as Justice Gorsuch mentioned , emphasized in his his\\nopinion in in June Medical , that the problem\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 61.913162,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"abortion regulation . Contrary to the state's arguments here , at the time of the founding and for most of early American history , women had an an ability to access abortion in the early\\nHeritage Reporting Corporation\\nOfficial\\nstages of pregnancy , and it was only when the\\nfetus was deemed sufficiently separate the\\nstates could act to bar that .\\nSo I think that the viability line\\nalso aligns with history and tradition in that\\nrespect .\\nCHIEF JUSTICE ROBERTS : Justice\\nThomas ?\\nJUSTICE THOMAS : You heard my question\\nto counsel earlier about the woman who was\\nconvicted of criminal child neglect . What would be your reaction to that as far as her liberty and whether or not the liberty interest that\\nwe're talking about extends to her ?\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 59.821438,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"understand that , but , if\\nI were to ask you what constitutional right\\nprotects the right to abortion , is it privacy ?\\nIs it autonomy ? What would it be ?\\nHeritage Reporting Corporation\\nOfficial\\nMS .\\nRIKELMAN :\\nIt's liberty , Your\\nHonor . It's the textual protection in the\\nFourteenth Amendment that a state can't deprive a person of liberty without due process of law , and the Court has interpreted liberty to include the right to make family decisions and the right to physical autonomy , including the right to end a pre - viability pregnancy .\\nJUSTICE THOMAS : So it's all of the\\nabove ?\\nMS . RIKELMAN : Well , the Court\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 58.1913,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"right , Justice Sotomayor , and I think that this is responsive as well to the questions that the Chief Justice was asking about , in particular , the impact of enforcing a 15 - week bar in this case . The Court has always looked at that issue by looking at the people for whom the law is a restriction , not those for whom it's irrelevant . So the question is , why would women\\nneed access to abortion after 15 weeks , and what is the effect on them ? And there are any number\\nof women who cannot get an abortion earlier .\\nThey don't realize that they're pregnant .\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 57.49886,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"MS . RIKELMAN : Your Honor , of course ,\\nthose issues aren't posed in this case , and , again , I would say that the states can certainly regulate throughout pregnancy , both before and after viability , to preserve fetal life and to\\nHeritage Reporting Corporation\\nOfficial\\npreserve the woman's health .\\nThe Court has said , however , there\\nis\\nthere are other constitutional issues at\\nstake , for instance , in the Ferguson case , that\\nstates still can't violate women's Fourth\\nAmendment rights . But , again , that's not what\\nthis case is about .\\nThis case is about a ban on abortion\\nthat the state concedes is weeks before\\nviability , and the Court has been clear for 50 years that the one thing that states cannot do\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 56.77557,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"unworkable for the courts . It would be both\\nless principled and less workable than\\n5 viability , and some of the reasons for that are ,\\nwithout viability , there will be no stopping\\npoint .\\nStates will rush to ban abortion at\\nvirtually any point in pregnancy . Mississippi itself has a six - week ban that it's defending with very similar arguments as it's using to defend the 15 - week ban . And there are states .\\nthat have bans\\nCHIEF JUSTICE ROBERTS : Well , I know ,\\nbut I'd like to focus on the 15 - week ban because that's not a dramatic departure from viability . It is the standard that the vast majority of\\nother countries have .\\nWhen you get to the viability\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 55.48428,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': 'supreme court has to decide as a matter of state constitutional law what the scope of an abortion right is . And the second trimester ends at 27 weeks . And so that state supreme court says , we think that the right exists , you know , in a in a in an absolute sense , that the state\\ncannot take away the right up to 27 weeks and\\nthen after that adopts an undue burden standard .\\nAs a matter of first principles , is\\nthat line acceptable as a matter of\\nconstitutional law ?\\nMS . RIKELMAN : Your Honor , it may be ,\\nbut I think the\\nthe question in this case is\\nwhether a line is obviously more principled or obviously more workable than viability because of the stare decisis context .\\nJUSTICE BARRETT : Why\\nI mean ,'},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 54.47169,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"of states or states still could or\\nand\\npresumably would continue to freely allow\\nabortion , many states ; some states would be able to do that even if you prevail under your view ,\\nis that correct ?\\nMR . STEWART : That's consistent with\\nour view ,\\nYour Honor . It's\\nit's one that\\nallows all interests to have full voice and\\nand many of the abortions we see in certain states that I don't think anybody would think would be moving to change their laws in a more restrictive direction .\\nJUSTICE KAVANAUGH : Thank you .\\nMR . STEWART :\\nThank you , Your Honor .\\nCHIEF JUSTICE ROBERTS : Justice\\nBarrett ?\\nJUSTICE BARRETT : General , I have a\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 53.6751,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"Britain , and most of Europe allows access to abortion right up until viability , and it also doesn't have the same barriers in place .\\nCHIEF JUSTICE ROBERTS : What do you\\nmean ,\\neven if they have nominal lines earlier ? MS . RIKELMAN : Some countries , Your\\nHonor , have a nominal line of 12 weeks or 18 weeks , but they permit legal access to abortion after that point for broad social reasons ,\\nhealth reasons , socioeconomic reasons , so their regimes really aren't comparable , and they also don't have the same type types of barriers\\nthat we have here . So , if\\nthe Court were to\\nmove the line substantial substantially\\nbackwards\\nand 15 weeks is 9 weeks before\\nviability , Your Honor , it's quite a bit backwards it may need to reconsider\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 52.363148,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"GENERAL PRELOGAR : Well , Justice\\nThomas , I have to confess that I haven't read\\nthe specific case you're referring to , but , if I understand the question you were posing , it\\nsounds as though the state is seeking to\\nregulate for a child that's been born that was\\ninjured while it was inside the womb .\\nAnd I think that we are not denying\\nthat a state has an interest there . We're not\\ndenying that a state has an interest here\\neither . Roe recognized that states have\\nHeritage Reporting Corporation\\nOfficial\\ninterests that exist from the outset of\\npregnancy .\\nBut , with respect to this specific\\nright to abortion , there are also profound liberty interests of the woman on the other side of the scale in not being forced to continue\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 51.63048,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"versus Casey haunt our country . They have no basis in the Constitution . They have no home in our history or traditions . They've damaged the democratic process . They've poisoned the law . They've choked off compromise .\\nFor 50 years , they've kept this Court\\nat the center of a political battle that it can never resolve . And 50 years on , they stand\\nalone . Nowhere else does this Court recognize a right to end a human life .\\nConsider this case : The Mississippi\\nlaw here prohibits abortions after 15 weeks .\\nThe law includes robust exceptions for a woman's life and health . It leaves months to obtain an\\nHeritage Reporting Corporation\\nOfficial\\nLO 5\\nabortion . Yet , the courts below struck the law\\ndown . It didn't matter that the law apply\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 50.83584,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"rights does nothing to diminish the impact on their liberty interest in forcing them to\\ncontinue with that pregnancy .\\nJUSTICE SOTOMAYOR : Thank you .\\nCHIEF JUSTICE ROBERTS : General ,\\nfollowing up on that , would that argument be true in terms of viability as well ? In other words , what your discussion of the reliance interests and the ability of women and men to control their lives in reliance on the right to\\nto an abortion , the argument would not be as strong , I think you'll have to concede , given what we're talking about , which is not a\\nprohibition ; it's a 15 - week line .\\nIs that right ?\\nGENERAL PRELOGAR : Yes . So this\\nHeritage Reporting Corporation\\nOfficial\\nCHIEF JUSTICE ROBERTS : There\\nyou\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 49.70284,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"that this Court has the authority to order the states to prohibit abortion as I understand it ,\\ncorrect ?\\nMR . STEWART : Correct , Your Honor .\\nJUSTICE KAVANAUGH : And as I\\nunderstand it , you're arguing that the\\nConstitution's silent and , therefore , neutral on the question of abortion ? In other words , that the Constitution's neither pro - life nor\\npro - choice on the question of abortion but\\nleaves the issue for the people of the states or perhaps Congress to resolve in the democratic\\nprocess ? Is that accurate ?\\nMR . STEWART : Right . We're\\nwe're\\nHeritage Reporting Corporation\\nOfficial\\nsaying it's left to the people , Your Honor .\\nJUSTICE KAVANAUGH :\\nAnd so ,\\nfor the\\nif you were to prevail , the states , a majority\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 48.830803,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"GENERAL PRELOGAR : The right is\\ngrounded in the liberty component of the Fourteenth Amendment , Justice Thomas , but I\\nthink that it promotes interests in autonomy , bodily integrity , liberty , and equality . And I do think that it is specifically the right to abortion here , the right of a woman to be able to control , without the state forcing her to continue a pregnancy , whether to carry that baby\\nto term .\\nJUSTICE THOMAS : I understand we're\\ntalking about abortion here , but what is\\nconfusing is that we\\nif we were talking about\\nthe Second Amendment , I know exactly what we're\\ntalking about . If we're talking about the\\nFourth Amendment , I know what we're talking about because it's written . It's there .\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 46.480556,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"under Casey . So , if the Court upholds this law under the undue burden standard , it would be carrying forward with those features , which I\\nI and I hope I've answered your question , but\\nI think that's one of the very strong reasons to\\nHeritage Reporting Corporation\\nOfficial\\njust go all the way and overrule Roe and Casey ,\\nYour Honor . I\\nanyway .\\nCHIEF JUSTICE ROBERTS : Justice\\nGorsuch ?\\nJustice Kavanaugh ?\\nJUSTICE KAVANAUGH : I want to be clear\\nabout what you're arguing and not arguing .\\nMR . STEWART : Yes , Your Honor .\\nJUSTICE KAVANAUGH : And to be clear ,\\nyou're not arguing that the Court somehow has the authority to itself prohibit abortion or\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 44.946987,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"You know , in your petition for cert ,\\nyour first question and the only one on which we granted review was whether all pre - viability\\nprohibitions on elective abortions are\\nunconstitutional . And then I think it's fair to\\nsay that when you got to the brief on the\\nmerits , you kind of shifted gears and talked a lot more about whether or not Roe and Casey\\nshould be overruled . And I wanted to give you a chance to explain that .\\nMR . STEWART : Sure , Your Honor . So a\\ncouple points . You know , at the petition stage ,\\nwe'd\\nwe were , of course , identifying identified for the Court three questions .\\nWe\\nemphasized , as you do at the cert stage , hey , this is important ; only this Court can resolve\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 44.15782,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"That's especially true of women who are young or don't have haven't experienced a pregnancy before , or their life circumstances change , as you referred to , Justice Sotomayor . They lose their job or their relationship breaks apart or\\nHeritage Reporting Corporation\\nOfficial\\nthey have medical complications . Or , for many\\nwomen , they don't have the resources to pay for\\nit earlier . It takes time for them to raise the\\nmoney or make the appropriate logistical\\narrangements to be able to take time off work and travel and have childcare . And for all\\nthose women in this category who need access\\nto abortion after 15 weeks , the fact that other\\nwomen were able to exercise their constitutional\"},\n",
              " {'documentId': 'd7a12571-a029-4270-98ac-35746e5272e9',\n",
              "  'score': 43.528587,\n",
              "  'sourceUrl': 'https://eyelevel-upload.s3.us-west-2.amazonaws.com/prod/file/1e761d6f-e91b-438f-bbdb-e2d6d906955f/Dobbs ',\n",
              "  'text': \"out of 37 states at the time when the Fourteenth\\nAmendment was adopted . Is that correct ?\\nMS . RIKELMAN : That is correct because\\nsome of the states had started to discard the\\ncommon law at that point because of a\\ndiscriminatory view that a woman's proper role was as a wife and mother , a view that the\\nConstitution now rejects , and that's why it's appropriate to do the historical analysis at a higher level of generality .\\nJUSTICE ALITO : Right . In the face of\\nthat , can it said that the right to\\nto\\nabortion is deeply rooted in the history and\\ntraditions of the American people ?\\nMS . RIKELMAN : Yes , it can , Your\\nHonor . Again , at the founding , women were able\\nto end their pregnancy under the common law .\"}]"
            ]
          },
          "metadata": {},
          "execution_count": 22
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Augmentation"
      ],
      "metadata": {
        "id": "0yntBg9Pu6b6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "results = content_response.body[\"search\"]\n",
        "\n",
        "maxInstructCharacters = 3000\n",
        "\n",
        "llmText = \"\"\n",
        "for r in results[\"results\"]:\n",
        "  if \"text\" in r and len(r[\"text\"]) > 0:\n",
        "    if len(llmText) + len(r[\"text\"]) > maxInstructCharacters:\n",
        "      break\n",
        "    elif len(llmText) > 0:\n",
        "      llmText += \"\\n\"\n",
        "    llmText += r[\"text\"]\n",
        "\n",
        "llmText"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 400
        },
        "id": "Nz9nsnBcvBZX",
        "outputId": "5e86a2a0-bedd-4553-cb88-d7d3d3b99bbc"
      },
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"that the law applies when an unborn child is undeniably human , when risks to women surge , and when the common abortion procedure is brutal .\\nThe lower courts held that because the law\\nprohibits abortions before viability , it is unconstitutional no matter what .\\nRoe and Casey's core holding ,\\naccording to those courts , is that the people can protect an unborn girl's life when she just barely can survive outside the womb but not any earlier when she needs a little more help . That is the world under Roe and Casey .\\nThat is not the world the Constitution\\npromises . The Constitution places its trust in the people . On hard issue after hard issue , the people make this country work . Abortion is a\\noverrule the stare decisis section of Casey , you say . And that that's that's what I think\\nis being brought up , and maybe I haven't made it\\nclearer ,\\nbut I've tried to .\\nMS . RIKELMAN : Yes , Your Honor . I\\nthink the point that the Court was making was that the fact that some states may continue to enact laws in the teeth of the Court's precedent has never been enough of a reason to overrule .\\nAnd that's true for a number of decisions that\\nthe Court has issued . The fact that some people continue to disagree with them is not a basis to discard that precedent .\\nCHIEF JUSTICE ROBERTS : Justice\\nThomas , anything further ?\\nJUSTICE THOMAS : Back to my original\\nquestion .\\nIf I were\\nI know your interest\\nhere is in abortion , I\\nchanged , that's not a point in Roe and Casey's favor . They have no basis in the Constitution . They they adopt a right that purposefully leads to the termination of now millions of\\nhuman lives . The\\nif nothing had changed ,\\nthey'd be just as bad as they were 30 years ago , 50 years ago . And now we just have decades of damage , and we have a situation where nearly 30 years after Casey , the Court unfortunately divides over what Casey , the lead case on on\\nin the abortion area , even means .\\nThe lower courts are left not knowing , as I think and I think kind of a\\nwhat to do\\nfundamental problem here is , I think , as Justice Gorsuch mentioned , emphasized in his his\\nopinion in in June Medical , that the problem\\nabortion regulation . Contrary to the state's arguments here , at the time of the founding and for most of early American history , women had an an ability to access abortion in the early\\nHeritage Reporting Corporation\\nOfficial\\nstages of pregnancy , and it was only when the\\nfetus was deemed sufficiently separate the\\nstates could act to bar that .\\nSo I think that the viability line\\nalso aligns with history and tradition in that\\nrespect .\\nCHIEF JUSTICE ROBERTS : Justice\\nThomas ?\\nJUSTICE THOMAS : You heard my question\\nto counsel earlier about the woman who was\\nconvicted of criminal child neglect . What would be your reaction to that as far as her liberty and whether or not the liberty interest that\\nwe're talking about extends to her ?\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Generation"
      ],
      "metadata": {
        "id": "MrnclwwwvB-J"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from openai import OpenAI\n",
        "\n",
        "instruction =  \"You are a helpful virtual assistant that answers questions using the content below. Your task is to create detailed answers to the questions by combining your understanding of the world with the content provided below. Do not share links.\"\n",
        "\n",
        "client = OpenAI()\n",
        "\n",
        "completion = client.chat.completions.create(\n",
        "  model=\"gpt-3.5-turbo-0301\",\n",
        "  messages=[\n",
        "    {\n",
        "      \"role\": \"system\",\n",
        "      \"content\": \"\"\"%s\n",
        "===\n",
        "%s\n",
        "===\n",
        "\"\"\"\n",
        "      % (instruction, llmText),\n",
        "    },\n",
        "    {\"role\": \"user\", \"content\": \"What arguments were posed in banning abortions?\"},\n",
        "  ],\n",
        ")"
      ],
      "metadata": {
        "id": "yuPk1iLsuhPf"
      },
      "execution_count": 28,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "completion.choices[0].message.content"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 122
        },
        "id": "tzKk6aduv5PO",
        "outputId": "e04658d9-d2aa-4105-d2a0-f2961977a926"
      },
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\"The arguments posed in banning abortions are that the law applies when an unborn child is undeniably human, when risks to women surge, and when the common abortion procedure is considered brutal. The lower courts held that prohibiting abortions before viability is unconstitutional regardless of the circumstances. However, some argue that this overrules the principle of stare decisis in Casey, meaning that some states may continue to enact laws in the teeth of the Court's precedent and never be enough of a reason to overrule the law. The Constitution places its trust in the people, according to some, and on hard issue after hard issue, the people make this country work. Abortion is a fundamental problem here as it purposefully leads to the termination of millions of human lives and has no basis in the Constitution.\""
            ],
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            }
          },
          "metadata": {},
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "B39FBFsSxeuy"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}